<script setup>
import {useRouter} from 'vue-router'  /*导入路由器函数*/

import { Check, Delete, Edit, Link } from "@element-plus/icons-vue";

import { reactive, ref } from "vue";

const router=useRouter(); /*创建路由器 */

const ruleFormRef = ref();

const strHide=ref("display:block")

const checkAge = (rule, value, callback) => {
  if (!value) {
    return callback(new Error("Please input the age"));
  }
  setTimeout(() => {
    if (!Number.isInteger(value)) {
      callback(new Error("Please input digits"));
    } else {
      if (value < 18) {
        callback(new Error("Age must be greater than 18"));
      } else {
        callback();
      }
    }
  }, 1000);
};

const validateAccount = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("账号不能为空"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule, value, callback) => {
  if (value === "") {
    callback(new Error("Please input the password again"));
  } else if (value !== ruleForm.pass) {
    callback(new Error("Two inputs don't match!"));
  } else {
    callback();
  }
};

const ruleForm = reactive({
  account: "",
  checkPass: "",
  age: "",
});

const rules = reactive({
  account: [{ validator: validateAccount, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
  age: [{ validator: checkAge, trigger: "blur" }],
});

const submitForm = (formEl) => {
  // if (!formEl) return;
  // formEl.validate((valid) => {
  //   if (valid) {
  //     console.log("submit!");
  //   } else {
  //     console.log("error submit!");
  //     return false;
  //   }
  // });

  //1.隐藏登录框
    strHide.value='display:none'
  //2.跳转到后台页面
  //alert(11110000)

  //请求后端服务，将用户登录的账号和密码发给后台，后台校验成功后，返回id 否则返回-1
  router.push("home")
};

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};
</script>

<template>
  <div id="box" :style="strHide">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      status-icon
      :rules="rules"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="account">
        <el-input v-model="ruleForm.account" type="text" autocomplete="off" />
      </el-form-item>

      <el-form-item label="密码" prop="checkPass">
        <el-input
          v-model="ruleForm.checkPass"
          type="password"
          autocomplete="off"
        />
      </el-form-item>

      <el-form-item label="角色" prop="age">
        <el-select v-model="ruleForm.age" placeholder="请选择身份">
          <el-option label="教师" value="0" />
          <el-option label="管理员" value="1" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)"
          >登录</el-button
        >
        <el-button @click="resetForm(ruleFormRef)">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>
#box {
  width: 400px;
  height: 280px;
  margin: 50px auto;
  text-align: center;
}
</style>
